home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Solitaire / Sources / Klondike / Klondike.m < prev    next >
Text File  |  1994-04-21  |  8KB  |  276 lines

  1. /* indent:4  tabsize:8  font:fixed-width */
  2.  
  3. #import "Klondike.h"
  4. #import "localstrings.h"
  5. #import "KlondikePrefs.h"
  6. #import "DrawPileDelegate.h"
  7. #import "GamePileDelegate.h"
  8. #import "SuitPileDelegate.h"
  9. #import "DiscardPileDelegate.h"
  10.  
  11. @implementation Klondike
  12.  
  13.  
  14. /*---------------------------------------------------------------------------
  15. |
  16. |    - startGame:
  17. |
  18. |    returns:    (id)  self
  19. |
  20. |----------------------------------------------------------------------------
  21. |
  22. |    Start a new game.
  23. |            
  24. \----------------------------------------------------------------------------*/
  25.  
  26. - startGame:sender
  27. {
  28.     [super startGame:sender];
  29.     return [self setupGame:YES];
  30. }
  31.  
  32.  
  33. /*---------------------------------------------------------------------------
  34. |
  35. |    - restartGame:
  36. |
  37. |    returns:  (id) self
  38. |
  39. |----------------------------------------------------------------------------
  40. |
  41. |    Restart the game in progress (i.e. don't shuffle the cards).
  42. |            
  43. \----------------------------------------------------------------------------*/
  44.  
  45. - restartGame:sender
  46. {
  47.     [super restartGame:sender];
  48.     return [self setupGame:NO];    
  49. }
  50.  
  51.  
  52. /*---------------------------------------------------------------------------
  53. |
  54. |    - setupGame:(BOOL)redeal
  55. |
  56. |    returns:  (id) self
  57. |
  58. |----------------------------------------------------------------------------
  59. |
  60. |    Setup a new game.  If "redeal" is true, deal a new deck, otherwise
  61. |    use the same cards as the previous game.
  62. |            
  63. \----------------------------------------------------------------------------*/
  64. - setupGame:(BOOL)redeal
  65. {
  66.     int pileIndex, cardIndex , i;
  67.     id  suitCardPiles[4];
  68.     id  gameCardPiles[7];
  69.     id  drawCardPile;
  70.  
  71.     [gameWindow disableFlushWindow];
  72.  
  73.     [drawPileDelegate setDrawCardCount:[prefs cardsToDraw]];
  74.     [discardPileDelegate setDrawCardCount:[prefs cardsToDraw]];
  75.     
  76.     //----------------------------------------------------------------
  77.     //    Sync pile id's with current game window and set 
  78.     //    preferences and delegates
  79.     //----------------------------------------------------------------
  80.     
  81.     suitCardPiles[0] = suitPileView1;
  82.     suitCardPiles[1] = suitPileView2;
  83.     suitCardPiles[2] = suitPileView3;
  84.     suitCardPiles[3] = suitPileView4;
  85.     for (i = 0; i < 4; i++)
  86.     {
  87.         [suitCardPiles[i] setBackgroundColor:desktopColor];
  88.         [suitCardPiles[i] setCardSize:cardSize];
  89.         [suitCardPiles[i] setDelegate:suitPileDelegate];
  90.     }
  91.     
  92.     gameCardPiles[0] = gamePileView1;
  93.     gameCardPiles[1] = gamePileView2;
  94.     gameCardPiles[2] = gamePileView3;
  95.     gameCardPiles[3] = gamePileView4;
  96.     gameCardPiles[4] = gamePileView5;
  97.     gameCardPiles[5] = gamePileView6;
  98.     gameCardPiles[6] = gamePileView7;
  99.     for (i = 0; i < 7; i++)
  100.     {
  101.         [gameCardPiles[i] setBackgroundColor:desktopColor];
  102.         [gameCardPiles[i] setCardSize:cardSize];
  103.         [gameCardPiles[i] setDelegate:gamePileDelegate];
  104.     }
  105.     
  106.     [gamePileDelegate setSuitCardPileViews:suitCardPiles];
  107.     
  108.     [drawPileView setBackgroundColor:desktopColor];
  109.     [drawPileView setCardSize:cardSize];
  110.     drawCardPile = [drawPileView cardPile];
  111.     [drawPileDelegate setDiscardCardPileView:discardPileView];
  112.     [drawPileView setDelegate:drawPileDelegate];
  113.     
  114.     [discardPileView setBackgroundColor:desktopColor];
  115.     [discardPileView setCardSize:cardSize];
  116.     [discardPileView setDelegate:discardPileDelegate];
  117.     [discardPileDelegate setSuitCardPileViews:suitCardPiles];
  118.  
  119.     [gameWindow display];
  120.     [gameWindow reenableFlushWindow];
  121.     [gameWindow flushWindow];
  122.  
  123.  
  124.     //-----------------------------------------------------------------------
  125.     //    Empty the recycle pile
  126.     //-----------------------------------------------------------------------
  127.     
  128.     [recycleCardPile freeCards];
  129.  
  130.     //-----------------------------------------------------------------------
  131.     //    Initialize the drawPileView to have a shuffled
  132.     //    deck
  133.     //-----------------------------------------------------------------------
  134.     [drawCardPile freeCards];
  135.     if (redeal)
  136.     {
  137.         [[drawCardPile addDeck] shuffle];
  138.     
  139.     // make a copy of the CardPile for restart option
  140.     if (!prevDeck)
  141.     {
  142.         prevDeck = [[CardPile allocFromZone:[self zone]]
  143.                                initForCardSize:cardSize];
  144.     }
  145.     else
  146.     {
  147.         [prevDeck freeCards];
  148.         [prevDeck setCardSize:cardSize];
  149.     }
  150.     [prevDeck addCopyOfPile:drawCardPile];
  151.     }
  152.     else
  153.     {
  154.         if (prevDeck)
  155.     {
  156.         // copy the saved deck back to the game deck
  157.         [prevDeck setCardSize:cardSize];
  158.         [drawCardPile addCopyOfPile:prevDeck];
  159.     }
  160.     else
  161.     {
  162.         // this shouldn't happen, but just in case...
  163.             [[[drawCardPile freeCards] addDeck] shuffle];
  164.     }
  165.     }
  166.  
  167.     
  168.     //----------------------------------------------------------------------
  169.     //    Initialize the discardPileView as empty
  170.     //----------------------------------------------------------------------
  171.  
  172.     [[discardPileView cardPile] freeCards];
  173.     [discardPileView display];
  174.  
  175.     //-----------------------------------------------------------------------
  176.     //    Initialize the four "suit piles" as empty
  177.     //-----------------------------------------------------------------------
  178.  
  179.     for (pileIndex = 0; pileIndex < 4; pileIndex++)
  180.     {
  181.         [[suitCardPiles[pileIndex] cardPile] freeCards];
  182.         [suitCardPiles[pileIndex] display];
  183.     }
  184.  
  185.     //-----------------------------------------------------------------------
  186.     //    Initialize and deal cards to the seven "user piles"
  187.     //-----------------------------------------------------------------------
  188.  
  189.     for (pileIndex = 0; pileIndex < 7; pileIndex++)
  190.     {
  191.         id userPile = [gameCardPiles[pileIndex] cardPile];
  192.         
  193.         [userPile freeCards];
  194.     [gameCardPiles[pileIndex] display];
  195.     }
  196.  
  197.     for (pileIndex = 0; pileIndex < 7; pileIndex++)
  198.     {
  199.         id userPile = [gameCardPiles[pileIndex] cardPile];
  200.         
  201.         for (cardIndex = 0; cardIndex <= pileIndex; cardIndex++)
  202.         {
  203.             id tempCard = [drawCardPile cardAt:CS_TOP];
  204.             
  205.             [drawCardPile removeCard:tempCard];
  206.             [userPile insertCard:tempCard at:CS_TOP];
  207.         [gameCardPiles[pileIndex] display];
  208.         }
  209.         [[userPile cardAt:CS_TOP] flip];
  210.         [gameCardPiles[pileIndex] display];
  211.     }
  212.  
  213.     //-----------------------------------------------------------------------
  214.     //    Draw the remaining cards on the
  215.     //    drawPileView
  216.     //-----------------------------------------------------------------------
  217.  
  218.     //[drawPileView display];
  219.     
  220.     [gameWindow display];
  221.  
  222.     [gameWindow makeKeyAndOrderFront:self];
  223.     return self;
  224. }
  225.  
  226.  
  227. /*---------------------------------------------------------------------------
  228. |
  229. |    - endGame:sender
  230. |
  231. |    returns:  (id) self
  232. |
  233. |----------------------------------------------------------------------------
  234. |
  235. |    End the game in progress.  Discard the game window.
  236. |            
  237. \----------------------------------------------------------------------------*/
  238.  
  239. - endGame:sender
  240. {
  241.     [super endGame:sender];
  242.  
  243.     // ****custom code here****
  244.     
  245.     return self;
  246. }
  247.  
  248.  
  249. /*---------------------------------------------------------------------------
  250. |
  251. |    - checkForWin
  252. |
  253. |    returns: (id) self
  254. |
  255. |----------------------------------------------------------------------------
  256. |
  257. |    Called to check the state of the game.
  258. |
  259. \----------------------------------------------------------------------------*/
  260.  
  261. - checkForWin
  262. {
  263.     if ([[suitPileView1 cardPile] cardCount] +
  264.             [[suitPileView2 cardPile] cardCount] +
  265.             [[suitPileView3 cardPile] cardCount] +
  266.             [[suitPileView4 cardPile] cardCount] == 52)
  267.     {
  268.         [self win];
  269.     }
  270.     return self;
  271. }
  272.  
  273.  
  274. @end
  275.  
  276.